package com.ztorn.fiscale.service.impl;

import java.util.List;

import com.ztorn.common.core.utils.DateUtils;
import com.ztorn.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ztorn.fiscale.domain.FiscaleDocument;
import com.ztorn.fiscale.domain.FiscaleDocumentRelation;
import com.ztorn.fiscale.mapper.FiscaleDocumentRelationMapper;
import com.ztorn.fiscale.service.IFiscaleDocumentRelationService;

/**
 * 单证关联Service业务层处理
 *
 * @author ztorn
 */
@Service
public class FiscaleDocumentRelationServiceImpl implements IFiscaleDocumentRelationService
{
    @Autowired
    private FiscaleDocumentRelationMapper fiscaleDocumentRelationMapper;

    /**
     * 查询单证关联
     *
     * @param relationId 单证关联ID
     * @return 单证关联
     */
    @Override
    public FiscaleDocumentRelation selectFiscaleDocumentRelationById(Long relationId)
    {
        return fiscaleDocumentRelationMapper.selectFiscaleDocumentRelationById(relationId);
    }

    /**
     * 查询单证关联列表
     *
     * @param fiscaleDocumentRelation 单证关联
     * @return 单证关联
     */
    @Override
    public List<FiscaleDocumentRelation> selectFiscaleDocumentRelationList(FiscaleDocumentRelation fiscaleDocumentRelation)
    {
        return fiscaleDocumentRelationMapper.selectFiscaleDocumentRelationList(fiscaleDocumentRelation);
    }

    /**
     * 查询单证的关联单证列表
     *
     * @param documentId 单证ID
     * @return 关联单证集合
     */
    @Override
    public List<FiscaleDocument> selectRelatedDocumentList(Long documentId)
    {
        return fiscaleDocumentRelationMapper.selectRelatedDocumentList(documentId);
    }

    /**
     * 新增单证关联
     *
     * @param fiscaleDocumentRelation 单证关联
     * @return 结果
     */
    @Override
    public int insertFiscaleDocumentRelation(FiscaleDocumentRelation fiscaleDocumentRelation)
    {
        fiscaleDocumentRelation.setCreateTime(DateUtils.getNowDate());
        fiscaleDocumentRelation.setCreateBy(SecurityUtils.getUsername());
        return fiscaleDocumentRelationMapper.insertFiscaleDocumentRelation(fiscaleDocumentRelation);
    }

    /**
     * 修改单证关联
     *
     * @param fiscaleDocumentRelation 单证关联
     * @return 结果
     */
    @Override
    public int updateFiscaleDocumentRelation(FiscaleDocumentRelation fiscaleDocumentRelation)
    {
        fiscaleDocumentRelation.setUpdateTime(DateUtils.getNowDate());
        fiscaleDocumentRelation.setUpdateBy(SecurityUtils.getUsername());
        return fiscaleDocumentRelationMapper.updateFiscaleDocumentRelation(fiscaleDocumentRelation);
    }

    /**
     * 批量删除单证关联
     *
     * @param relationIds 需要删除的单证关联ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleDocumentRelationByIds(Long[] relationIds)
    {
        return fiscaleDocumentRelationMapper.deleteFiscaleDocumentRelationByIds(relationIds);
    }

    /**
     * 删除单证关联信息
     *
     * @param relationId 单证关联ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleDocumentRelationById(Long relationId)
    {
        return fiscaleDocumentRelationMapper.deleteFiscaleDocumentRelationById(relationId);
    }

    /**
     * 批量添加单证关联
     *
     * @param documentId 单证ID
     * @param relatedDocumentIds 关联单证ID数组
     * @return 结果
     */
    @Override
    @Transactional
    public int batchAddDocumentRelations(Long documentId, Long[] relatedDocumentIds)
    {
        int count = 0;
        for (Long relatedDocumentId : relatedDocumentIds)
        {
            // 检查是否已存在关联
            FiscaleDocumentRelation relation = new FiscaleDocumentRelation();
            relation.setDocumentId(documentId);
            relation.setRelatedDocumentId(relatedDocumentId);

            List<FiscaleDocumentRelation> existingRelations = selectFiscaleDocumentRelationList(relation);
            if (existingRelations.isEmpty())
            {
                // 不存在则添加
                insertFiscaleDocumentRelation(relation);
                count++;
            }
        }
        return count;
    }
}
